#include "PreCompiled.h"
#include "Command.h"
#include "Action.h"
#include "MainWindow.h"
#include "BitmapFactoryInst.h"
CommandBase::CommandBase(const char* sMenu, const char* sToolTip, const char* sWhat,
						 const char* sStatus, const char* sPixmap, const char* sAcc)
						 : sMenuText(sMenu), sToolTipText(sToolTip), sWhatsThis(sWhat?sWhat:sToolTip),
						 sStatusTip(sStatus?sStatus:sToolTip), sPixmap(sPixmap), sAccel(sAcc), _pcAction(0)
{

}

CommandBase::~CommandBase()
{

}

Action* CommandBase::getAction()const
{
	return _pcAction;
}
Action* CommandBase::createAction()
{
   return 0;
}

void CommandBase::setWhatsThis(const char*s)
{
   sWhatsThis = _strdup(s);
}
void CommandBase::setMenuText(const char*s)
{
    sMenuText = _strdup(s);
}
void CommandBase::setToolTipText(const char*s)
{
   sToolTipText = _strdup(s);
}
void CommandBase::setStatusTip(const char*s)
{
   sStatusTip = _strdup(s);
}
void CommandBase::setPixmap(const char*s)
{
   sPixmap = _strdup(s);
}
void CommandBase::setAccel(const char*s)
{
   sAccel = _strdup(s);
}

// ------------------------------------------------- [4/12/2013 Administrator]
Command::Command(const char* name)
:CommandBase(0),sName(name)
{
	sAppModule  = "FreeCAD";
	sGroup      = QT_TR_NOOP("Standard");
}

Command::~Command(void)
{
}

Action* Command::createAction()
{
	Action* pcAction;
	pcAction = new Action(this,getMainWindow());
	applyCommandData(pcAction);
	if(sPixmap)
		pcAction->setIcon(BitmapFactory().pixmap(sPixmap));
	pcAction->setShortcut(QString::fromAscii(sAccel));

	return pcAction;
}

void Command::applyCommandData(Action* action)
{
	action->setText(QCoreApplication::translate(this->className(),sMenuText,0,QCoreApplication::UnicodeUTF8));

	action->setToolTip(QCoreApplication::translate(this->className(),sToolTipText,0,QCoreApplication::UnicodeUTF8));

	if(sStatusTip)
		action->setStausTip(QCoreApplication::translate(this->className(),sStatusTip,0,QCoreApplication::UnicodeUTF8));
	else
		action->setStausTip(QCoreApplication::translate(this->className(),sToolTipText,0,QCoreApplication::UnicodeUTF8));

	if(sWhatsThis)
		action->setWhatThis(QCoreApplication::translate(this->className(),sWhatsThis,0,QCoreApplication::UnicodeUTF8));
	else
		action->setWhatThis(QCoreApplication::translate(this->className(),sToolTipText,0,QCoreApplication::UnicodeUTF8));

	
}

const char* Command::keySequenceToAccel(int sk)const
{

	QKeySequence::StandardKey type = (QKeySequence::StandardKey)  sk;
	QKeySequence ks(type);
	QString qs = ks.toString();
	QByteArray data = qs.toAscii();
#if defined (_MSC_VER)
	return _strdup((const char*)data);
#else
	return strdup((const char*)data);
#endif
 
}
bool Command::_blockCmd = false;
void Command::testActive()
{
	if(!_pcAction)return;
	if(_blockCmd)
	{
		_pcAction->setEnabled(false);
		return;
	}

  bool bActive = isActive();
  _pcAction->setEnabled(bActive);
}
void Command::invoke(int i)
{
   if(isActive())
	   activated(i);
}
void Command::addTo(QWidget* pcWidget)
{
	if(!_pcAction)
		_pcAction = createAction();
	_pcAction->addTo(pcWidget);
}


// -------------------------------------------------- [4/12/2013 Administrator]
CommandManager::CommandManager()
{

}
CommandManager::~CommandManager()
{

}

void CommandManager::addCommand(Command* cmd)
{
   _sCommands[cmd->getName()] = cmd;
}
void CommandManager::removeCommand(Command* cmd)
{
	std::map<std::string,Command*>::iterator it = _sCommands.find(cmd->getName());
	if(it != _sCommands.end())
	{
		delete it->second;
		_sCommands.erase(it);
	}

}

bool CommandManager::addTo(const char* Name,QWidget* pcWidget)
{

	if(_sCommands.find(Name) == _sCommands.end())
	{
		return false;
	}
	else
	{
		Command* pCommand = _sCommands[Name];
		pCommand->addTo(pcWidget);
		return true;
	}


}

std::vector<Command*> CommandManager::getAllCommand(void)const
{
	std::vector<Command*> vCmds;
	for(std::map<std::string,Command*>::const_iterator it = _sCommands.begin(); it != _sCommands.end(); it++)
	{
		vCmds.push_back(it->second);
	}
	return vCmds;
}

Command* CommandManager::getCommandByName(const char* sName)const
{
	std::map<std::string, Command*>::const_iterator it = _sCommands.find(sName);
	return (it != _sCommands.end())?it->second:0;
}

void CommandManager::runCommandByName(const char* sName)const
{
   Command* cmd = getCommandByName(sName);
	   if(cmd)
	   {
		   cmd->invoke(0);
	   }
}

void  CommandManager::clearCommand()
{
	for (std::map<std::string, Command*>::iterator it = _sCommands.begin();it != _sCommands.end(); it++)
   {
	   delete it->second;
	   it->second = NULL;
   }
	_sCommands.clear();
      
}

void CommandManager::testActive()
{
	for(std::map<std::string,Command*>::iterator it = _sCommands.begin(); it != _sCommands.end(); it++)
	{
       it->second->testActive();
	}
}